package com.ssps.back.platform.service.Impl;

import com.ssps.back.platform.service.ComplaintService;
import com.ssps.entity.*;
import com.ssps.mapper.ComplaintMapper;
import com.ssps.mapper.UserInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author li
 * @time 2020/07/23 20:10:09
 */

@Service
public class ComplaintServiceImpl implements ComplaintService {

    @Autowired
    ComplaintMapper complaintMapper;
    @Autowired
    UserInfoMapper userInfoMapper;

    @Override
    public List<Complaint> complaintFindAll(String status, String compluser, String responuser) {
        ComplaintExample example=new ComplaintExample();
        ComplaintExample.Criteria criteria = example.createCriteria();
        UserInfoExample infoExample=new UserInfoExample();
        UserInfoExample.Criteria infoExampleCriteria = infoExample.createCriteria();
        List<Integer> com=new ArrayList<>();
        List<Integer> res=new ArrayList<>();
        if (status!=""&&status!=null){
            criteria.andStatusEqualTo(status);
        }
        if (compluser!=""&&compluser!=null){
        infoExampleCriteria.andUnameLike("%"+compluser+"%");
        List<UserInfo> userInfoList=userInfoMapper.selectByExample(infoExample);
            if (userInfoList != null) {
                for (UserInfo userInfo : userInfoList) {
                    com.add(userInfo.getUiid());
                }
                criteria.andComuserIn(com);
            }

        }
        if (responuser!=""&&responuser!=null){
            infoExampleCriteria.andUnameLike("%"+responuser+"%");
            List<UserInfo> userInfoList=userInfoMapper.selectByExample(infoExample);
            if (userInfoList != null) {
                for (UserInfo userInfo : userInfoList) {
                    res.add(userInfo.getUiid());
                }
                criteria.andResuserIn(res);
            }

        }
        List<Complaint> complaintList=complaintMapper.selectByExample(example);
        if (complaintList!=null){
            for (Complaint complaint : complaintList) {
                UserInfo compluserInfo=userInfoMapper.selectByPrimaryKey(complaint.getComuser());
                UserInfo responuseInfo=userInfoMapper.selectByPrimaryKey(complaint.getResuser());
                complaint.setCompluser(compluserInfo);
                complaint.setResponuser(responuseInfo);
            }
        }
        return complaintList;
    }

    @Override
    public void complaintUpdate(Complaint complaint) {
        complaintMapper.updateByPrimaryKeySelective(complaint);
    }
}
